package com.yangchoi.jetpackcompose.ui.main.page

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.google.accompanist.flowlayout.FlowRow
import com.yangchoi.jetpackcompose.theme.white_16sp
import com.yangchoi.jetpackcompose.ui.baseui.BaseComposePage
import com.yangchoi.jetpackcompose.ui.main.viewmodel.ClassicEvent
import com.yangchoi.jetpackcompose.ui.main.viewmodel.ClassicViewModel
import com.yangchoi.jetpackcompose.ui.widget.component.CommonTopBar
import com.yangchoi.jetpackcompose.ui.widget.component.DefaultLine
import com.yangchoi.jetpackcompose.ui.widget.ext.onClick
import com.yangchoi.lib_base.network.entity.ClassicChildItemEntity
import com.yangchoi.lib_base.network.entity.ClassicItemEntity

@Composable
fun ClassicPage(navHostController: NavHostController,viewModel: ClassicViewModel = hiltViewModel()){

    val viewState = viewModel.viewState
    val classicList = viewState.classicList
    val childList = viewState.childList


    var selectIndex by remember { mutableStateOf(0) }

    BaseComposePage(
        topBarContent = {
           CommonTopBar(title = "知识体系", showBackIcon = false)
        },
        onResume = {
            viewModel.dispatch(event = ClassicEvent.OnInitClassic)
        }
    ) {
        classicList?.let {
            Row(modifier = Modifier.fillMaxWidth()) {
                LazyColumn(modifier = Modifier
                    .width(100.dp)
                    .fillMaxHeight()){
                    itemsIndexed(classicList){ index, item ->
                        LeftBoxItem(item,selectIndex,index, onItemClick = {
                            selectIndex = it
                            viewModel.dispatch(event = ClassicEvent.OnChangeChildData, position = selectIndex)
                        })
                    }
                }
                FlowRow(modifier = Modifier
                    .padding(start = 10.dp)
                    .fillMaxSize()
                    .padding(top = 10.dp)
                ){
                    childList?.forEach {
                        Box(modifier = Modifier.padding(end = 10.dp, bottom = 10.dp).background(color = Color.Gray.copy(alpha = 0.4f), shape = RoundedCornerShape(12.dp))){
                            Text(text = "${it.name}", style = white_16sp, modifier = Modifier.padding(horizontal = 10.dp, vertical = 3.dp))
                        }
                    }
                }
            }
        }
    }
}

@Composable
private fun LeftBoxItem(item:ClassicItemEntity,selectIndex:Int,itemIndex:Int,onItemClick:(Int) -> Unit){
    Box(modifier = Modifier
        .fillMaxWidth()
        .height(48.dp)
        .onClick {
            onItemClick.invoke(itemIndex)
        }
        .background(color = if (selectIndex == itemIndex) Color.Gray.copy(alpha = 0.5f) else Color.White)){

        Text(text = "${item.name}", style = TextStyle(fontSize = 14.sp, color = if (selectIndex == itemIndex) Color.Red else Color.Black),
            modifier = Modifier
                .padding(horizontal = 5.dp)
                .align(Alignment.Center)
        )

        Box(modifier = Modifier.align(Alignment.BottomCenter)){
            DefaultLine()
        }
    }
}